#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 100000;

struct person {
    char name[6];
    int year;
    int month;
    int day;
}P[MAX+10];

bool cmp(person a, person b) {
    if (a.year != b.year) return a.year > b.year;
    else if (a.month != b.month) return a.month > b.month;
    else return a.day > b.day; 
}

int main() {
    int n, count = 0;
    char name[6];
    int year, month, day;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s %d/%d/%d", name, &year, &month, &day);
        if (year > 2014-200 && year < 2014) {
            strcpy(P[count].name, name);
            P[count].year = year;
            P[count].month = month;
            P[count++].day = day;
        } else if (year == 2014) {
            if (month < 9) {
                strcpy(P[count].name, name);
                P[count].year = year;
                P[count].month = month;
                P[count++].day = day;
            } else if (month == 9) {
                if (day <= 6) {
                    strcpy(P[count].name, name);
                    P[count].year = year;
                    P[count].month = month;
                    P[count++].day = day;
                }
            }
        } else if (year == 2014 - 200) {
            if (month > 9) {
                strcpy(P[count].name, name);
                P[count].year = year;
                P[count].month = month;
                P[count++].day = day;
            } else if (month == 9) {
                if (day >= 6) {
                    strcpy(P[count].name, name);
                    P[count].year = year;
                    P[count].month = month;
                    P[count++].day = day;
                }
            }
        }
    }

    sort(P, P+count, cmp);

    if (count != 0) {
        printf("%d %s %s\n", count, P[count-1].name, P[0].name);
    } else {
        printf("0\n");
    }

    return 0;
}